package com.third.clock.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.third.clock.MainActivity;
import com.third.clock.R;
import com.third.clock.ui.model.ILoginView;
import com.third.clock.ui.model.presenter.LoginPresenter;

/**
 * Created by Administrator on 2018/7/23 0023.
 */

public class LoginActivity extends CheckPermissionsActivity implements ILoginView,View.OnClickListener{

    private Button mLoginButton;

    private EditText mEditUserName;

    private EditText mEditpassword;

    private ImageView mImageViewClear;

    private ImageView mImageViewPwdSwitch;

    private boolean isShowPassword;

    LoginPresenter mLoginPresenter=null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initEvent();
    }

    private void initView(){
        mLoginButton= (Button) findViewById(R.id.btn_login);
        mEditUserName= (EditText) findViewById(R.id.et_userName);
        mEditpassword=(EditText) findViewById(R.id.et_password);
        mImageViewClear= (ImageView) findViewById(R.id.iv_unameClear);
        mImageViewPwdSwitch=(ImageView) findViewById(R.id.iv_pwdswitch);
    }

    private void initEvent(){
        mLoginPresenter=new LoginPresenter(LoginActivity.this);
        mLoginPresenter.setILoginView(this);

        mLoginButton.setOnClickListener(this);
        mImageViewClear.setOnClickListener(this);
        mImageViewPwdSwitch.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                String username=mEditUserName.getText().toString().trim();
                String password=mEditpassword.getText().toString().trim();
                mLoginPresenter.login(username,password);
                break;
            case R.id.iv_unameClear:
                mEditUserName.setText("");
                break;
            case R.id.iv_pwdswitch:
                if (!isShowPassword) {
                    //如果选中，显示密码
                    mEditpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mImageViewPwdSwitch.setImageResource(R.mipmap.login_look);
                } else {
                    //否则隐藏密码
                    mEditpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mImageViewPwdSwitch.setImageResource(R.mipmap.login_unlook);
                }
                isShowPassword=!isShowPassword;
                break;
        }
    }

    @Override
    public void onLoginning() {

    }

    @Override
    public void onHideLoginning() {

    }

    @Override
    public void onLoginFail(String message) {
         if(!TextUtils.isEmpty(message)){
             new SVProgressHUD(LoginActivity.this).showInfoWithStatus(message);
         }
    }

    @Override
    public void onLoginSuccess() {
       startActivity(new Intent(LoginActivity.this, MainActivity.class));
       this.finish();
    }
}
